package com.xiaoma.tpo.utils.baidupush;

import net.sf.json.JSONObject;

import com.baidu.yun.core.log.YunLogEvent;
import com.baidu.yun.core.log.YunLogHandler;
import com.baidu.yun.push.auth.PushKeyPair;
import com.baidu.yun.push.client.BaiduPushClient;
import com.baidu.yun.push.constants.BaiduPushConstants;
import com.baidu.yun.push.exception.PushClientException;
import com.baidu.yun.push.exception.PushServerException;
import com.baidu.yun.push.model.PushMsgToAllRequest;
import com.baidu.yun.push.model.PushMsgToAllResponse;
import com.baidu.yun.push.model.PushMsgToSingleDeviceRequest;
import com.baidu.yun.push.model.PushMsgToSingleDeviceResponse;
import com.baidu.yun.push.model.PushRequest;

public class BDPush {
    private static final String apiKey = "T2pDZ2EwXu7GHXQFBVeten2G";
    private static final String secretKey = "Nbwhiy4ZidwuaG6vORuY6TCdcQXUHUzy";
    private static final int IOS_ENV = 1; //1:开发。 2.生产。
    private static BaiduPushClient pushClient;
    
    static {
        PushKeyPair pair = new PushKeyPair(apiKey, secretKey);
        pushClient = new BaiduPushClient(pair, BaiduPushConstants.CHANNEL_REST_URL);
        pushClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });
    }
    
    public static void iosPush2Single(String channelId, IOSPushMsg iosMsg) throws PushClientException, PushServerException {
        JSONObject notification = new JSONObject();
        JSONObject jsonAPS = new JSONObject();
        jsonAPS.put("alert", iosMsg.getTitle());
        notification.put("aps", jsonAPS);
        notification.accumulateAll(iosMsg.getContent());
        PushMsgToSingleDeviceRequest request = new PushMsgToSingleDeviceRequest()
        .addChannelId(channelId)
        .addMsgExpires(new Integer(3600)) // 设置message的有效时间
        .addMessageType(1)// 1：通知,0:透传消息.默认为0 注：IOS只有通知.
        .addMessage(notification.toString())
        .addDeployStatus(IOS_ENV). // IOS(使用) 1: Developer. 2: Production.
        addDeviceType(4);// deviceType => 3:android, 4:ios
        pushMsg(request);
    }
    public static void iosPush2All(IOSPushMsg iosMsg) throws PushClientException, PushServerException {
        JSONObject notification = new JSONObject();
        JSONObject jsonAPS = new JSONObject();
        jsonAPS.put("alert", iosMsg.getTitle());
        notification.put("aps", jsonAPS);
        notification.accumulateAll(iosMsg.getContent());
        PushMsgToAllRequest request = new PushMsgToAllRequest()
        .addMsgExpires(new Integer(3600))
        .addMessageType(1)
        .addMessage(notification.toString())
        .addDepolyStatus(IOS_ENV)
        .addDeviceType(4);
        pushMsg(request);
    }
    public static void androidPush2Single(String channelId, AndroidPushMsg androidMsg) throws PushClientException, PushServerException {
        JSONObject notification = new JSONObject();
        notification.put("title", androidMsg.getTitle());
        notification.put("description",androidMsg.getDescription());
        notification.put("custom_content", androidMsg.getCustomContent());
        PushMsgToSingleDeviceRequest request = new PushMsgToSingleDeviceRequest()
        .addChannelId(channelId)
        .addMsgExpires(new Integer(3600)). // message有效时间
        addMessageType(1).// 1：通知,0:透传消息. 默认为0 注：IOS只有通知.
        addMessage(notification.toString()).
        addDeviceType(3);// deviceType => 3:android, 4:ios
        pushMsg(request);
    }
    public static void androidPush2All(AndroidPushMsg androidMsg) throws PushClientException, PushServerException {
        JSONObject notification = new JSONObject();
        notification.put("title", androidMsg.getTitle());
        notification.put("description",androidMsg.getDescription());
        notification.put("custom_content", androidMsg.getCustomContent());
        PushMsgToAllRequest request = new PushMsgToAllRequest()
        .addMsgExpires(new Integer(3600))
        .addMessageType(1)
        .addMessage(notification.toString())
        .addDeviceType(3);
        pushMsg(request);
    }
    
    private static void pushMsg(PushRequest request) throws PushClientException, PushServerException {
        try {
            if (request.getClass().equals(PushMsgToSingleDeviceRequest.class)) {//单推
                PushMsgToSingleDeviceResponse response = pushClient.pushMsgToSingleDevice((PushMsgToSingleDeviceRequest)request);
                System.out.println("msgId: " + response.getMsgId() + ",sendTime: " + response.getSendTime());
            } else {//全推
                PushMsgToAllResponse response = pushClient.pushMsgToAll((PushMsgToAllRequest)request);
                System.out.println("msgId: " + response.getMsgId() + ",sendTime: " + response.getSendTime() + ",timerId: " + response.getTimerId());
            }
        } catch (PushClientException e) {
            // ERROROPTTYPE 用于设置异常的处理方式 -- 抛出异常和捕获异常,'true' 表示抛出, 'false' 表示捕获。
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                e.printStackTrace();
            }
        } catch (PushServerException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                System.out.println(String.format("requestId: %d, errorCode: %d, errorMessage: %s",e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
            }
        }
        
    }
    
    public static void main(String[] args) throws PushClientException, PushServerException {
        
    }
   
}
